package am.aua.renju.bussinesslayer;

import am.aua.renju.bussinesslayer.exception.EmailAlreadyRegistredException;
import am.aua.renju.bussinesslayer.exception.InvalidInputException;
import am.aua.renju.bussinesslayer.exception.NicknameAlreadyRegistredException;
import am.aua.renju.bussinesslayer.validator.EmailValidator;
import am.aua.renju.databaseaccess.GameOperations;
import am.aua.renju.databaseaccess.IUserOperations;
import am.aua.renju.databaseaccess.Operations;
import am.aua.renju.databaseaccess.UserOperations;
import am.aua.renju.entity.SecurityQuestion;
import am.aua.renju.entity.Player;
import java.util.List;

/**
 *
 * @author Ara Yeritsian
 */
public class UserManager {
    
    private static UserManager instance;
    private IUserOperations userOperations;    
    
    public static UserManager getInstance() {
        if (instance == null) {
            instance = new UserManager();
        }
        return instance;
    }
    
    private UserManager(){
        userOperations = UserOperations.getInstance();    
    }
    
    public String getNickname(String email){
        return userOperations.getUser(email).getNickname();        
    }

    public boolean autenticate(String email, String password) {
        Player user = userOperations.getUser(email);
        if (user != null && user.getPassword().equals(password)) {
            return true;
        }
        return false;
    }

    /* register new user*/
    public void register(String email, String password, String nickname,
            String securityQuestionText, String answer)
            throws EmailAlreadyRegistredException, NicknameAlreadyRegistredException, InvalidInputException {

        SecurityQuestion securityQuestion = Operations.getInstance().
                getSecurityQuestionByQuestionString(securityQuestionText);

        EmailValidator emv = new EmailValidator();
        if (!emv.validate(email) || securityQuestion == null || answer.length() < 3
                || nickname.length() < 3 || password.length() < 3) {
            throw new InvalidInputException();
        }

        Player user = userOperations.getUser(email);
        if (user != null) {
            throw new EmailAlreadyRegistredException();
        }

        user = userOperations.getUserByNickname(nickname);
        if (user != null) {
            throw new NicknameAlreadyRegistredException();
        }

        Player newUser = new Player();
        newUser.setId(1); //JPA requirement for persisting new user. Even though in database it is autoincrement.
        newUser.setEmail(email);
        newUser.setPassword(password);
        newUser.setSecurityQuestionId(securityQuestion);
        newUser.setSecurityQuestionAnswer(answer);
        newUser.setNickname(nickname);
        newUser.setRating(1000);
        userOperations.createUser(newUser);
    }

    public List<Player> getTop10Players() {
        return UserOperations.getInstance().getTop10Players();
    }
}
